<template>
    <div class="home-recommondSongs">
        <div class="tabs">
            <div>
                <van-icon name="replay" size="1.2rem" @click="reload()" />
                <span style="font-size: 1.2rem;margin-left: 0.2rem;">热门歌曲推荐</span>
            </div>
            <div @click="play(swipePageData[0][0])">
                <van-icon name="play" size="0.5rem" />
                <a>播放</a>
            </div>
        </div>
        <van-swipe class="list" :loop="false">
            <van-swipe-item v-for="(item, index) in swipePageData" :key="index">
                <div v-for="(songInfo, index) in item" :key="index" :title="songInfo" class="song"
                    @click="play(songInfo)">
                    <van-image :src="songInfo.al.picUrl" radius="0.7rem" class="album" />
                    <div class="infoAll">
                        <div class="info">
                            <div>
                                <span>{{ songInfo.name }}</span>
                            </div>
                            <div class="info-singer">
                                <a>-</a>
                                <a v-for="(singer, index) in songInfo.ar" :key="index">
                                    {{ singer.name }}
                                    <span v-show="index != songInfo.ar.length - 1">/</span>
                                </a>
                            </div>
                        </div>
                        <div class="albums">
                            <span>{{ songInfo.al.name }}</span>
                        </div>
                        <div class="underLine">

                        </div>
                    </div>
                </div>
            </van-swipe-item>
        </van-swipe>
    </div>
</template>

<script >
export default {
    data() {
        return {
            listData: [],
            swipePageData: [],
            dataAll: [],
        }
    },
    created() {
        this.getSongList();
        window.that = this;
    },
    methods: {
        getSongList() {
            this.$request('get', '/playlist/detail?id=3778678').then(res => {
                // console.log(res)
                if (res.code == 200) {
                    // this.listData = res.playlist.tracks
                    let num = this.getRandom(0, 188)
                    this.dataAll = res.playlist.tracks
                    this.listData = res.playlist.tracks.slice(num, num + 12)
                    // console.log(this.listData)
                    let group = []
                    for (let i = 0; i < 4; i++) {
                        group = this.listData.splice(0, 3);
                        this.swipePageData.push(group);
                    }
                    // console.log(this.swipePageData)
                }
            });
        },
        getRandom(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min;
        },
        play(item) {
            // console.log(item.name, item.ar[0].name, item.id, item.al.picUrl)
            this.$store.dispatch("serviceSetPlayData", { "title": item.name, "singer": item.ar[0].name, id: item.id, headUrl: item.al.picUrl, right: true })
        },
        reload() {
            this.swipePageData = []
            let group = []
            let num = this.getRandom(0, 188)
            this.listData = this.dataAll.slice(num, num + 12)
            for (let i = 0; i < 4; i++) {
                group = this.listData.splice(0, 3);
                this.swipePageData.push(group);
            }
        }

    }

}
</script>

<style scoped>
.tabs {
    margin: 0.1rem 0.8rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.tabs>div:first-child {
    display: flex;
    align-items: center;
}

.tabs>div:last-child {
    display: flex;
    align-items: center;
    justify-content: center;
    border: solid 1px lightgray;
    border-radius: 1rem;
    width: 3rem;
    height: 1.2rem;
}

.tabs>div>a {
    color: black;
    font-size: 0.7rem;
    /* color: rgba(26, 26, 26, .5); */
}


.album {
    width: 3rem;
    height: 3rem;
}


.list {
    height: 17rem;
}

.song {
    width: 95%;
    display: flex;
    align-items: center;
    margin-bottom: 0.8rem;
    margin-left: 0.5rem;

}

.infoAll {
    width: 80%;
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: flex-start;
    flex-direction: column;
    overflow: hidden;
    text-overflow: ellipsis;

}

.albums {
    margin-bottom: 0.3rem;
    margin-left: 0.5rem;
    white-space: nowrap;
    font-size: 0.7rem;
}

.info {
    display: flex;
    align-items: center;
    height: 2.5rem;
    margin-left: 0.5rem;
    white-space: nowrap;
    width: 80%;
}

.underLine {
    width: 90%;
    border-bottom: 1px solid #d9d9d9;
}

.info-singer {
    overflow: hidden;
    text-overflow: ellipsis;
    color: lightgray;
}

.info-singer>* {
    display: inline-block;
    font-size: 0.7rem;
    margin-left: 0.3rem;
}

.song>* {
    margin: 0 0.2rem;
}
</style>